package com.ezjie.ielts.module_read.adapter;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.Map;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.ezjie.ielts.R;
import com.ezjie.ielts.constant.Constants;
import com.ezjie.ielts.model.FullTextInfo;
import com.ezjie.ielts.model.GroupInfo;
import com.ezjie.ielts.model.QuestionInfo;
import com.ezjie.ielts.model.SubmitGroupInfo;
import com.ezjie.ielts.module_read.ReadAnswerAnlysisActivity;
import com.ezjie.ielts.view.NoScrollGridView;
import com.ezjie.ielts.widget.SerializableMap;
import com.umeng.analytics.MobclickAgent;

public class ReadInstantReportAdapter extends BaseAdapter {

	private ArrayList<GroupInfo> mList = new ArrayList<GroupInfo>();
	private Context mContext;
	private ArrayList<FullTextInfo> origindata = new ArrayList<FullTextInfo>();
	private Map<Integer, GroupInfo> mInstantReportGroupInfoList = new LinkedHashMap<Integer, GroupInfo>(); // 即时报告所需数据
	private Map<Integer, SubmitGroupInfo> mSubmitReadAnswer = new LinkedHashMap<Integer, SubmitGroupInfo>();

	public ReadInstantReportAdapter(Context mContext, Bundle bundle) {
		this.mContext = mContext;

		SerializableMap serializableMap = (SerializableMap) bundle
				.getSerializable(Constants.READ_INSTANT_REPORT_ANSWER);

		mInstantReportGroupInfoList = serializableMap.getMap();

		SerializableMap serializableMap2 = (SerializableMap) bundle
				.getSerializable(Constants.READ_INSTANT_REPORT_SUBMIT_ANSWER);

		mSubmitReadAnswer = serializableMap2.getMap();

		origindata = (ArrayList<FullTextInfo>) bundle.getSerializable(Constants.READ_SETDATA_ORIGIN);

		for (Integer key : mInstantReportGroupInfoList.keySet()) {
			mList.add(mInstantReportGroupInfoList.get(key));
		}
	};

	@Override
	public int getCount() {
		return mList.size();
	}

	@Override
	public GroupInfo getItem(int position) {
		return mList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;
		final GroupInfo info = getItem(position);
		if (null == convertView) {

			holder = new ViewHolder();
			convertView = View.inflate(mContext, R.layout.layout_read_instant_report_item, null);
			holder.tv_read_instant_report_question = (TextView) convertView
					.findViewById(R.id.tv_read_instant_report_question);
			holder.tv_read_instant_report_content = (TextView) convertView
					.findViewById(R.id.tv_read_instant_report_content);
			holder.gv_read_instant_report_img = (NoScrollGridView) convertView
					.findViewById(R.id.gv_read_instant_report_img);

			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		holder.tv_read_instant_report_question.setText(info.group_name);
		holder.tv_read_instant_report_content.setText(info.content);
		NoScrollGridViewAdapter adapter = new NoScrollGridViewAdapter(info.questions_list);

		holder.gv_read_instant_report_img.setAdapter(adapter);

		holder.gv_read_instant_report_img.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int pos, long id) {
				if(TextUtils.isEmpty(getItem(position).audio)){
					// 友盟统计
					MobclickAgent.onEvent(mContext, "Feedback_clickIcon");
				}else{
					MobclickAgent.onEvent(mContext, "listenFeedback_clickIcon");
				}
				Intent intent = new Intent(mContext, ReadAnswerAnlysisActivity.class);
				Bundle bundle = new Bundle();
				bundle.putSerializable(Constants.READ_SETDATA_SINGLE_ORIGIN, origindata.get(0));
				bundle.putSerializable(Constants.READ_INSTANT_REPORT_ANSWER, mList.get(position));
				bundle.putSerializable(Constants.READ_INSTANT_REPORT_SUBMIT_ANSWER,
						mSubmitReadAnswer.get(Integer.valueOf(info.g_id)));
				bundle.putInt(Constants.READ_POSITION, pos);
				intent.putExtra(Constants.READ_INSTANT_REPORT_BUNDLE, bundle);
				mContext.startActivity(intent);
			}
		});

		convertView.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if(TextUtils.isEmpty(getItem(position).audio)){
					// 友盟统计
					MobclickAgent.onEvent(mContext, "Feedback_clickCard");
				}else{
					MobclickAgent.onEvent(mContext, "listenFeedback_clickCard");
				}
				Intent intent = new Intent(mContext, ReadAnswerAnlysisActivity.class);
				Bundle bundle = new Bundle();
				bundle.putSerializable(Constants.READ_SETDATA_SINGLE_ORIGIN, origindata.get(0));
				bundle.putSerializable(Constants.READ_INSTANT_REPORT_ANSWER, mList.get(position));
				bundle.putSerializable(Constants.READ_INSTANT_REPORT_SUBMIT_ANSWER,
						mSubmitReadAnswer.get(Integer.valueOf(info.g_id)));
				intent.putExtra(Constants.READ_INSTANT_REPORT_BUNDLE, bundle);
				mContext.startActivity(intent);
			}
		});

		return convertView;
	}

	class ViewHolder {
		TextView tv_read_instant_report_question;
		TextView tv_read_instant_report_content;
		NoScrollGridView gv_read_instant_report_img;
	}

	class NoScrollGridViewAdapter extends BaseAdapter {

		private ArrayList<QuestionInfo> list;

		public NoScrollGridViewAdapter(ArrayList<QuestionInfo> list) {
			this.list = list;
		}

		@Override
		public int getCount() {
			return list.size();
		}

		@Override
		public QuestionInfo getItem(int position) {
			return list.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {

			ViewHolder holder = null;
			QuestionInfo info = getItem(position);
			if (null == convertView) {

				holder = new ViewHolder();
				convertView = View.inflate(mContext, R.layout.layout_instant_report_grid_view_item, null);
				holder.tv_read_question_no = (TextView) convertView.findViewById(R.id.tv_read_question_no);
				holder.iv_read_question_is_right = (ImageView) convertView.findViewById(R.id.iv_read_question_is_right);

				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}

			holder.tv_read_question_no.setText(info.no + ".");
			if (Boolean.valueOf(info.is_right)) {
				holder.iv_read_question_is_right.setImageResource(R.drawable.read_right_icon);
			} else {
				holder.iv_read_question_is_right.setImageResource(R.drawable.read_worng_icon);
			}

			return convertView;
		}

		class ViewHolder {
			TextView tv_read_question_no;
			ImageView iv_read_question_is_right;
		}

	}
}
